﻿using Approval.Domain;
using Approval.Infrastructure.Interface;
using Approval.ReadAPI.Appliaction.Command.PurchaseRelationCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PublicCommon;

namespace Approval.ReadAPI.Appliaction.CommandHandler.PurchaseRelationCommandHandler
{
    public class PurchaseRelationListCommandHandler : IRequestHandler<PurchaseRelationListCommand, List<PurchaseRelation>>
    {
        private readonly IPurchaseRelationRepository purchaseRelationRepository;

        public PurchaseRelationListCommandHandler(IPurchaseRelationRepository purchaseRelationRepository)
        {
            this.purchaseRelationRepository = purchaseRelationRepository;
        }

        public async Task<List<PurchaseRelation>> Handle(PurchaseRelationListCommand request, CancellationToken cancellationToken)
        {
            var res = await purchaseRelationRepository.GetListInfo(x => x.PId.Equals(request.PId));
            var list = res.OrderByDescending(x => x.Id);
            return list.ToList();
        }
    }
}
